/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.command;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.LazyInitializationException;
import org.junit.Test;
import org.openinsula.vulcano.core.command.transaction.TransactionInvoker;
import org.openinsula.vulcano.orm.command.DaoCommandFactory.TransactionCommand;
import org.openinsula.vulcano.orm.command.transaction.InitializeCallback;
import org.openinsula.vulcano.orm.jpa.entity.CompositeEntity;
import org.openinsula.vulcano.orm.jpa.entity.SimpleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = { "/test-context.xml", "classpath*:META-INF/*-context.xml" })
public class DaoCommandFactoryTestCase extends AbstractJUnit4SpringContextTests {

	@Autowired
	private DaoCommandFactory daoCommandFactory;

	@Autowired
	private TransactionInvoker transactionInvoker;

	@Test
	public void testInitializeCallback() {
		CompositeEntity compositeEntity = new CompositeEntity();
		
		transactionInvoker.invoke(daoCommandFactory.createSaveCommand(compositeEntity));
		assertNull(compositeEntity.getEntities());

		List<SimpleEntity> entities = new ArrayList<SimpleEntity>();
		
		for (int i = 0; i < 5; i++) {
			SimpleEntity simpleEntity = new SimpleEntity();
			simpleEntity.setName("Entity " + i);
			simpleEntity.setCompositeEntity(compositeEntity);
			entities.add(simpleEntity);
		}

		transactionInvoker.invoke(daoCommandFactory.createSaveCollectionCommand(entities));
		assertNull(compositeEntity.getEntities());
		
		TransactionCommand<CompositeEntity> command = daoCommandFactory.createRefreshCommand(compositeEntity);
		
		try {
			compositeEntity = transactionInvoker.invoke(command);
			compositeEntity.getEntities().size();
			fail("LazyInitializationException expected");
		}
		catch (LazyInitializationException e) {
		}
		
		command.setInitializeCallback(new InitializeCallback<CompositeEntity>() {

			@Override
			public void initialize(final CompositeEntity obj) {
				Hibernate.initialize(obj.getEntities());
			}
			
		});
		
		try {
			compositeEntity = transactionInvoker.invoke(command);
			compositeEntity.getEntities().size();
			
		}
		catch (LazyInitializationException e) {
			fail("LazyInitializationException unexpected");
		}
		
	}
	
//	@Test
	public void testSaveArray() {
		assertTrue(true); // keep import

		SimpleEntity[] entities = new SimpleEntity[] { new SimpleEntity(), new SimpleEntity(), new SimpleEntity(),
				new SimpleEntity(), new SimpleEntity() };

		transactionInvoker.invoke(daoCommandFactory.createSaveCommand(entities));
	}

//	@Test
	public void testSaveAllCollection() {
		Collection<SimpleEntity> entities = new ArrayList<SimpleEntity>();
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());

		transactionInvoker.invoke(daoCommandFactory.createSaveCollectionCommand(entities));
	}

//	@Test
	public void testDeleteAllByEntityClass() {
		transactionInvoker.invoke(daoCommandFactory.createDeleteTypeCommand(SimpleEntity.class));
	}

}
